package startup

import (
	"fmt"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/middleware"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/router"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/ycg/go2sky-plugins/gin/v3"
	"git.mycaigou.com/ycg/stark-extend/go2skyplugins"
	"git.myscrm.cn/golang/common/ykenv"
	"github.com/gin-gonic/gin"
)

const WELCOME = "micro-gfyx-api"

func init() {
	gin.SetMode(gin.DebugMode)
	if ykenv.IsTestMode() {
		gin.SetMode(gin.TestMode)
	}
	if ykenv.IsProdMode() || ykenv.IsReleaseMode() {
		gin.SetMode(gin.ReleaseMode)
	}
}

// RegisterHttpRoute 此处注册 gin 接口
func RegisterHttpRoute(r *gin.Engine) error {
	r.Use(gin.Recovery())
	r.Use(middleware.Cors())
	r.Use(middleware.AccessLogs())
	r.Use(middleware.LimitIP())

	// 注册自定义参数校验方法
	utils.RegisterValidatorCustomFunc()

	// 链路接入
	tracer, err := go2skyplugins.GetTracer()
	if err != nil {
		fmt.Println("go2skyplugins.GetTracer err:", err.Error())
	} else {
		r.Use(v3.Middleware(r, tracer)) // r是*gin.Engine
	}

	healthGroup := r.Group("/")
	router.RegisterHealthRouters(healthGroup)
	r.Any("/", func(c *gin.Context) {
		c.JSON(0, map[string]interface{}{
			"message": WELCOME,
		})
	})

	// rpa机器人
	rpaGroup := r.Group("/v1")
	rpaGroup.Use(middleware.AuthorizeRpa([]string{
		"/v1/rpa/start",
		"/v1/rpa/heartbeat",
		"/v1/rpa/init",
	}))
	router.RpaRegisterRouters(rpaGroup)

	// 聚合层
	aggregationGroup := r.Group("/v1")
	aggregationGroup.Use(middleware.AuthorizeNew(nil))
	router.AggregationRegisterRouters(aggregationGroup)

	// 网站信息
	siteGroup := r.Group("/v1")
	siteGroup.Use(middleware.AuthorizeNew(nil))
	router.SiteRegisterRouters(siteGroup)

	// 基础信息
	baseGroup := r.Group("/v1/base")
	baseGroup.Use(middleware.AuthorizeNew(
		[]string{
			"/v1/base/login/using-phone",
			"/v1/base/login/using-password",
			"/v1/base/login/generate-reset-password-token",
			"/v1/base/login/reset-password-by-phone",
			"/v1/base/login/sso",
			"/v1/base/login/refresh-token",
			"/v1/base/login/refresh-token",
			"/v1/base/login/release-locked",
		}),
	)

	router.BaseRegisterRouters(baseGroup)

	// 用户信息
	user := r.Group("/v1/user")
	user.Use(middleware.AuthorizeNew(nil))
	router.UserRegisterRouters(user)

	// 登录
	login := r.Group("/v1/login")
	router.LoginRegisterRouters(login)

	// 表单
	opportunityForm := r.Group("/v1/oppty/form")
	opportunityForm.Use(middleware.AuthorizeNew(nil))
	router.OpptyFormRegisterRouters(opportunityForm)

	// 报备
	opptyReport := r.Group("/v1/oppty/report")
	opptyReport.Use(middleware.AuthorizeNew(nil))
	router.OpptyReportRegisterRouters(opptyReport)

	// 报备跟进
	reportFollow := r.Group("/v1/oppty/report-follow")
	reportFollow.Use(middleware.AuthorizeNew(nil))
	router.OpptyReportFollowRegisterRouters(reportFollow)

	common := r.Group("/v1/common")
	common.Use(middleware.AuthorizeNew([]string{"/v1/common/verify-code/show-captcha", "/v1/common/verify-code/send-sms"}))
	router.CommonRegisterRouters(common)

	optionGroup := r.Group("/v1/option")
	optionGroup.Use(middleware.AuthorizeNew(nil))
	router.OptionRegisterRouters(optionGroup)

	contact := r.Group("/v1/contact")
	contact.Use(middleware.AuthorizeNew(nil))
	router.ContactRegisterRouters(contact)

	subscriptionGroup := r.Group("/v1/subscription")
	subscriptionGroup.Use(middleware.AuthorizeNew(nil))
	router.SubscriptionRegisterRouters(subscriptionGroup)

	// 按新的领域划分路径：/版本号/领域/聚合/动作
	businessOpportunityGroup := r.Group("/v1/business_opportunity")
	businessOpportunityGroup.Use(middleware.AuthorizeNew(nil))
	router.BusinessOpportunityRegisterRouters(businessOpportunityGroup)

	//线索领域
	clueGroup := r.Group("/v1/clue")
	clueGroup.Use(middleware.AuthorizeNew(nil))

	//线索管理
	clueManageGroup := clueGroup.Group("clue-manage")
	router.ClueRegisterRouters(clueManageGroup)
	// 客户管理
	customerManageGroup := clueGroup.Group("customer-manage")
	router.CustomerRegisterRouters(customerManageGroup)
	//客户监控
	clueCustomerGroup := clueGroup.Group("customer")
	router.ClueCustomerMonitorRegisterRouters(clueCustomerGroup)

	// 开放接口，目前仅提供接口给云采购平台调用、手动接口不鉴权
	openApiGroup := r.Group("/v1/openapi")
	openApiGroup.Use(middleware.AuthorizeOmp([]string{"" +
		"/v1/openapi/check-radar-profit",
		"/v1/openapi/hand-create-tenant-callback",
		"/v1/openapi/hand-create-tenant",
	}))
	router.OpenApiRegisterRouters(openApiGroup)

	mpGroup := r.Group("/v1/mini-program")
	mpGroup.Use(middleware.AuthorizeNew(nil))
	router.MpRegisterRouters(mpGroup)

	outboundGroup := r.Group("/v1/outbound")
	router.OutboundCallbackRegisterRouter(outboundGroup) //回调接口不需要登录态
	outboundGroup.Use(middleware.AuthorizeNew(nil))
	outboundGroup.Use(middleware.ProfitCheck(constants.ProfitAIOutbound))
	router.OutboundRegisterRouter(outboundGroup) //外呼的业务接口，需要登录态

	jobCenterGroup := r.Group("/v1/job-center")
	jobCenterGroup.Use(middleware.AuthorizeNew(nil))
	router.JobCenterRegisterRouter(jobCenterGroup)

	backendGroup := r.Group("/backend")
	backendGroup.Use(middleware.AuthorizeForZhanTu())
	router.BackendRegisterRouters(backendGroup)

	return nil
}
